#define _CRT_SECURE_NO_WARNINGS 1

#include"string.h"

namespace yyh
{
	const size_t string::npos = -1;

	string::string(const char* str)
		:_size(strlen(str))
	{
		_str = new char[_size + 1];
		_capacity = _size;
		strcpy(_str, str);
	}

	string::string(const string& s)
	{
		_str = new char[s._capacity + 1];
		strcpy(_str, s._str);
		_size = s._size;
		_capacity = s._capacity;
	}

	string& string::operator =(const string& s)
	{
		if (this != &s)
		{
			delete[] _str;
			_str = new char[s._capacity + 1];
			strcpy(_str, s._str);
			_size = s._size;
			_capacity = s._capacity;
		}
		return *this;
	}

	string::~string()
	{
		delete[] _str;
		_str = nullptr;
		_size = _capacity = 0;
	}

	void string::reserve(size_t n)
	{
		char* temp = new char[n + 1];
		strcpy(temp, _str);
		delete[] _str;
		_str = temp;
		_capacity = n;
	}

	void string::push_back(char ch)
	{
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 4 : 2 * _capacity);
		}
		_str[_size++] = ch;
		_str[_size] = '\0';
	}

	void string::append(const char* str)
	{
		size_t len = strlen(str);
		if (_size + len > _capacity)
		{
			size_t NewCapacity = 2 * _capacity;
			if (_size + len > NewCapacity)
			{
				NewCapacity = _size + len;
			}
			reserve(NewCapacity);
		}
		strcpy(_str + _size, str);
		_size += len;
	}

	string& string::operator +=(char ch)
	{
		push_back(ch);
		return *this;
	}

	string& string::operator +=(const char* str)
	{
		append(str);
		return *this;
	}

	void string::insert(size_t pos, size_t n, char ch)
	{
		assert(pos <= _size);
		assert(n > 0);

		if (_size + n > _capacity)
		{
			size_t NewCapacity = 2 * _capacity;
			if (_size + n > NewCapacity)
			{
				NewCapacity = _size + n;
			}
			reserve(NewCapacity);
		}
		size_t end = _size + n;
		while (end > pos + n - 1)
		{
			_str[end] = _str[end - n];
			end--;
		}
		for (size_t i = 0; i < n; i++)
		{
			_str[pos + i] = ch;
		}
		_size += n;
	}

	void string::insert(size_t pos, const char* str)
	{
		size_t len = strlen(str);
		insert(pos, len, 'x');
		for (size_t i = 0; i < len; i++)
		{
			_str[pos + i] = str[i];
		}
	}

	void string::erase(size_t pos, size_t len)
	{
		if (len > _size - pos)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else
		{
			size_t end = pos + len;
			while (end <= _size)
			{
				_str[end - len] = _str[end];
				end++;
			}
			_size -= len;
		}
	}

	size_t string::find(char ch, size_t pos)
	{
		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == ch)
			{
				return i;
			}
		}
		return npos;
	}

	size_t string::find(const char* str, size_t pos)
	{
		const char* p = strstr(_str + pos, str);
		if (p == nullptr)
		{
			return npos;
		}
		else
		{
			return p - _str;
		}
	}

	string string::substr(size_t pos, size_t len)
	{
		size_t LeftLen = _size - pos;
		if (len > LeftLen) 
		{
			len = LeftLen;
		}
		string temp;
		temp.reserve(len);
		for (size_t i = 0; i < len; i++)
		{
			temp += _str[pos + i];
		}
		return temp;
	}

	bool string::operator ==(const string& s) const
	{
		return strcmp(_str, s._str) == 0;
	}

	bool string::operator !=(const string& s) const
	{
		return !(*this == s);
	}

	bool string::operator <(const string& s) const
	{
		return strcmp(_str, s._str) < 0;
	}

	bool string::operator <=(const string& s) const
	{
		return *this == s || *this < s;
	}

	bool string::operator >(const string& s) const
	{
		return !(*this <= s);
	}

	bool string::operator >=(const string& s) const
	{
		return !(*this < s);
	}

	ostream& operator <<(ostream& out, const string& s)
	{
		for (auto e : s)
		{
			cout << e;
		}
		return out;
	}

	void test1()
	{
		string s1;
		cout << s1.c_str() << endl;
		string s2("hello world");
		cout << s2.c_str() << endl;

		s2 += 'y';
		s2 += "yyh";
		s2 += "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
		cout << s2.c_str() << endl;
	}

	void test2()
	{
		string s1("hello world");
		cout << s1.c_str() << endl;
		s1.insert(11, 3, 'x');
		cout << s1.c_str() << endl;
		s1.insert(6, 3, 'x');
		cout << s1.c_str() << endl;
		s1.insert(0, 3, 'x');
		cout << s1.c_str() << endl;

		string s2("hello world");
		cout << s2.c_str() << endl;
		s2.insert(11, "xxx");
		cout << s2.c_str() << endl;
		s2.insert(6, "xxx");
		cout << s2.c_str() << endl;
		s2.insert(0, "xxx");
		cout << s2.c_str() << endl;
	}

	void test3()
	{
		string s1("hello world");
		cout << s1.c_str() << endl;
		s1.erase(6, 3);
		cout << s1.c_str() << endl;
		s1.erase(0, 5);
		cout << s1.c_str() << endl;
		s1.erase();
		cout << s1.c_str() << endl;

		string s2("hello world");
		cout << s2.c_str() << endl;
		cout << s2.find('l') << endl;
		cout << s2.c_str() << endl;
		cout << s2.find("ld") << endl;
	}

	void test4()
	{
		string s1("12345678");
		for (size_t i = 0; i < s1.size(); i++)
		{
			s1[i]++;
			cout << s1[i] << ' ';
		}
		cout << endl;

		string::iterator it = s1.begin();
		while (it != s1.end())
		{
			cout << *it << ' ';
			it++;
		}
		cout << endl;

		for (auto e : s1)
		{
			cout << e << ' ';
		}
		cout << endl;

		const string s2("12345678");
		for (auto e : s2)
		{
			cout << e << ' ';
		}
		cout << endl;
	}

	void test5()
	{
		string s1("12345678");
		string s2 = s1.substr(3);
		cout << s2.c_str() << endl;
		s2 = s1;
		cout << s2.c_str() << endl;
		s2 = s2;
		cout << s2.c_str() << endl;
		cout << s1 << ' ' << s2 << endl;
	}
}